<template>

  <div class="auth-container">
            <div class="logo-box">
          <router-link to="/">
            <img src="/logo.png" alt="LOGO" class="logo">
          </router-link>
        </div>
    <form class="auth-form" @submit.prevent="handleSubmit" novalidate>
      <!-- 邮件地址输入项 -->
      <div class="form-group">
        <label class="form-label">
          <span class="required-mark">*</span> メールアドレス
        </label>
        <input 
          type="text" 
          class="form-input" 
          :class="{ 'error': errors.email }"
          v-model="form.email" 
          @blur="validateEmail"
          required
          style="width: 200px;"
        >
        <div v-if="errors.email" class="error-message">{{ errors.email }}</div>
      </div>

      <!-- 验证码输入项 -->
      <div class="form-group">
        <label class="form-label">
          <span class="required-mark">*</span> 検証コード
        </label>
        <div class="verification-group">
          <input 
            type="text" 
            class="form-input verification-input" 
            :class="{ 'error': errors.verificationCode }"
            v-model="form.verificationCode" 
            @blur="validateVerificationCode"
            required
            style="width: 130px;"
          >
          <button type="button" class="send-button" @click="send">
            <svg class="send-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-12a1 1 0 10-2 0v4a1 1 0 00.293.707l2.828 2.829a1 1 0 101.415-1.415L11 9.586V6z" clip-rule="evenodd" />
            </svg>
            送信
          </button>
        </div>
        <div v-if="errors.verificationCode" class="error-message">{{ errors.verificationCode }}</div>
      </div>

      <!-- 密码输入项 -->
      <div class="form-group">
        <label class="form-label">
          <span class="required-mark">*</span> パスワード
        </label>
        <input 
          type="password" 
          class="form-input" 
          :class="{ 'error': errors.password }"
          v-model="form.password" 
          @blur="validatePassword"
          required
          style="width: 200px;"
        >
        <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
        <p class="form-hint">※半角英数字8~32文字、記号利用可能</p>
      </div>

      <!-- 确认密码输入项 -->
      <div class="form-group1">
        <label class="form-label1">
          パスワードを再度入力してください
        </label>
        <input 
          type="password" 
          class="form-input" 
          :class="{ 'error': errors.confirmPassword }"
          v-model="form.confirmPassword" 
          @blur="validateConfirmPassword"
          required
          style="width: 200px;"
        >
        <div v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</div>
        <p class="form-hint1">※半角英数字8~32文字、記号利用可能</p>
      </div>

      <!-- 提交按钮 -->
      <div class="button-container">
        <button type="submit" class="update-button" @click="handleSubmit">
          ログイン情報を更新する
        </button>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

// 表单数据
const form = ref({
  email: '',
  verificationCode: '',
  password: '',
  confirmPassword: ''
});

// 错误状态
const errors = ref({
  email: '',
  verificationCode: '',
  password: '',
  confirmPassword: ''
});

// 验证邮箱
const validateEmail = () => {
  if (!form.value.email.trim()) {
    errors.value.email = '必須';
 
  }  else {
    errors.value.email = '';
  }
};

// 验证验证码
const validateVerificationCode = () => {
  if (!form.value.verificationCode.trim()) {
    errors.value.verificationCode = '必須';

  } else {
    errors.value.verificationCode = '';
  }
};

// 验证密码
const validatePassword = () => {
  if (!form.value.password.trim()) {
    errors.value.password = '必須';

  } else {
    errors.value.password = '';
  }
};

// 验证确认密码
const validateConfirmPassword = () => {
  if (!form.value.confirmPassword.trim()) {
    errors.value.confirmPassword = '必須';
  } else if (form.value.password !== form.value.confirmPassword) {
    errors.value.confirmPassword = '不一致';
  } else {
    errors.value.confirmPassword = '';
  }
};

// 验证所有字段
const validateAll = () => {
  validateEmail();
  validateVerificationCode();
  validatePassword();
  validateConfirmPassword();
  
  return !errors.value.email && !errors.value.verificationCode && 
         !errors.value.password && !errors.value.confirmPassword;
};

const send = () => {
    ElMessage.error('Request failed with status code 500');
    return;
  }

// 提交处理函数
const handleSubmit = (event) => {
  event.preventDefault();
  
  // 检查所有输入框是否为空
  if (!form.value.email.trim() && !form.value.verificationCode.trim() && 
      !form.value.password.trim() && !form.value.confirmPassword.trim()) {
    ElMessage.error('メールアドレスを入力してください。');
    return;
  }
  
  // 如果只输入了邮箱
  if (form.value.email.trim() && !form.value.verificationCode.trim() && 
      !form.value.password.trim() && !form.value.confirmPassword.trim()) {
    ElMessage.error('メールで受信した認証コードを入力してください。');
    return;
  }
  
  // 如果输入了邮箱和验证码，但没有密码
  if (form.value.email.trim() && form.value.verificationCode.trim() && 
      !form.value.password.trim() && !form.value.confirmPassword.trim()) {
    ElMessage.error('半角英数字8～32文字、記号利用可能');
    return;
  }
  
  // 如果输入了邮箱、验证码和密码，但没有确认密码
  if (form.value.email.trim() && form.value.verificationCode.trim() && 
      form.value.password.trim() && !form.value.confirmPassword.trim()) {
    ElMessage.error('半角英数字8～32文字、記号利用可能');
    return;
  }
  
  // 如果所有字段都有输入
  if (form.value.email.trim() && form.value.verificationCode.trim() && 
      form.value.password.trim() && form.value.confirmPassword.trim()) {
    ElMessage.error('認証コードが間違っています');
    return;
  }
};
</script>

<style scoped>
.logo-box {
  width: 100%;
  background-color: white;
  border-bottom: 1px solid rgb(181, 179, 179);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 0;
  min-height: 56px;
}
.logo {
    width: 105px;
    aspect-ratio: auto 105 / 35;
    height: 35px;
    position: relative;
    right: 475px;
}

.logo-container {
  position: relative;
  justify-content: center;
  margin-bottom: 15px;
  max-height: 20%;
}

.logo-container a {
  display: inline-block;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.logo-container a:hover {
  transform: scale(1.05);
}
.auth-container {
  /* display: flex; */
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  padding: 20px;
  background-color: #ffffff;
}

.auth-form {
  width: 100%;
  max-width: 1020px;
  padding: 40px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  background-color: #FCFCFC;
  margin-left: 210px;
}

.form-group {
  margin-bottom: 20px;
  margin-left: 100px;
}

.form-label {
  display: inline-block;
  width: auto;
  min-width: 120px;
  margin-right: 10px;
  font-size: 14px;
  color: #333333;
  vertical-align: middle;
  letter-spacing: 0.5px;
  white-space: nowrap;
}
.form-label1{
  display: inline-block;
  width: auto;
  min-width: 120px;
  font-size: 14px;
  color: #333333;
  vertical-align: middle;
  letter-spacing: 0.5px;
  white-space: nowrap;
}
.required-mark {
  color: #ff0000;
  margin-right: 4px;
}

.form-input {
  width: calc(100% - 130px);
  padding: 8px 12px;
  border: 1px solid #cccccc;
  border-radius: 4px;
  font-size: 14px;
  vertical-align: middle;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #66afe9;
  box-shadow: 0 0 0 1px rgba(102, 175, 233, 0.25);
}

.verification-group {
  display: inline-block;
  width: calc(100% - 130px);
  vertical-align: middle;
}

.verification-input {
  width: 65%;
  border-radius: 4px 0 0 4px;
  border-right: none;
}

.send-button {
  width:71px;
  height: 33px;
  padding: 8px 12px;
  border: 1px solid #cccccc;
  border-left: none;
  border-radius: 0 4px 4px 0;
  background-color: #efefef;
  font-size: 14px;
  color: #c3c1c1;
  cursor: pointer;
  vertical-align: top}

.send-button:hover {
  background-color: #e9e9e9;
}

.send-icon {
  width: 14px;
  height: 14px;
  /* margin-right: 4px; */
  /* vertical-align: middle; */
}

.form-hint {
  margin-top: 5px;
  margin-left: 130px;
  font-size: 12px;
  color: #666666;
}

.form-hint1 {
  margin-top: 5px;
  margin-left: 230px;
  font-size: 12px;
  color: #666666;
}
.button-container {
  display: flex;
  /* justify-content: start; */
  margin-top: 30px;
  position: relative;
  left: 230px;
}

.update-button {
  padding: 10px 30px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: #ffffff;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.update-button:hover {
  background-color: #0069d9;
}

/* 错误样式 */
.form-input.error {
  border-color: #dc3545;
  box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
}

.error-message {
  color: #dc3545;
  font-size: 12px;
  margin-top: 4px;
  margin-left: 130px;
  display: block;
}

/* 移动端自适应样式 */
@media (max-width: 768px) {
  .logo {
    position: static;
    right: auto;
    width: 80px;
    height: auto;
  }
  
  .auth-container {
    padding: 10px;
    min-height: auto;
  }
  
  .auth-form {
    margin-left: 0;
    padding: 20px;
    max-width: 100%;
    border: none;
    background-color: #ffffff;
  }
  
  .form-group {
    margin-left: 0;
    margin-bottom: 15px;
  }
  
  .form-label,
  .form-label1 {
    display: block;
    width: 100%;
    margin-bottom: 8px;
    margin-right: 0;
    min-width: auto;
  }
  
  .form-input {
    width: 100%;
    max-width: 100%;
  }
  
  .verification-group {
    width: 100%;
    display: flex;
    gap: 0;
  }
  
  .verification-input {
    flex: 1;
    width: auto;
    border-radius: 4px 0 0 4px;
  }
  
  .send-button {
    width: 80px;
    flex-shrink: 0;
  }
  
  .form-hint,
  .form-hint1 {
    margin-left: 0;
    margin-top: 5px;
  }
  
  .button-container {
    left: 0;
    justify-content: center;
    margin-top: 20px;
  }
  
  .update-button {
    width: 100%;
    max-width: 300px;
    padding: 12px 20px;
  }
  
  .error-message {
    margin-left: 0;
  }
}

@media (max-width: 480px) {
  .logo {
    width: 70px;
  }
  
  .auth-form {
    padding: 15px;
  }
  
  .form-label,
  .form-label1 {
    font-size: 13px;
  }
  
  .form-input {
    padding: 10px;
    font-size: 16px; /* 防止iOS缩放 */
  }
  
  .send-button {
    width: 70px;
    font-size: 12px;
    padding: 10px 8px;
  }
  
  .update-button {
    padding: 14px 20px;
    font-size: 16px;
  }
  
  .form-hint,
  .form-hint1 {
    font-size: 11px;
  }
}
</style>